#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/6 0006 21:22
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : forjump.py
import os
import numpy
import matplotlib.pyplot as plt
import PIL
import time
from PIL import Image
from matplotlib.animation import FuncAnimation

need_upd=True
def get_jump():
    os.system('adb shell screencap -p /sdcard/jump.png')
    os.system('adb pull /sdcard/jump.png')
    return numpy.array(PIL.Image.open('jump.png'))

def jump_to_next(point1,point2):
    x1,y1=point1
    x2,y2=point2
    distance=((x2-x1)**2+(y2-y1)**2)**0.5
    os.system('adb shell input swipe %s %s %s %s %s' % (x1, y1, x2, y2, int(distance * 1.35)))

def get_onclick(event,coor=[]):
    x , y = event.xdata , event.ydata
    coor.append((x,y))
    if len(coor)==2:
        jump_to_next(coor.pop(0),coor.pop(0))
    global need_upd
    need_upd=True

def upd_img(frame):
    global need_upd
    if need_upd:
        time.sleep(1)
        show_img.set_array(get_jump())
        need_upd=False
    return show_img,

figure=plt.figure()
show_img=plt.imshow(get_jump(),animated=True)
figure.canvas.mpl_connect('button_press_event',get_onclick)
ani=FuncAnimation(figure,upd_img,interval=50,blit=True)
plt.show()
